package com.antis.olsl.magic.rank;

import android.text.TextUtils;

import com.antis.olsl.base.net.BaseSubscriber;
import com.antis.olsl.base.net.ExceptionHandle;
import com.antis.olsl.base.utils.RxUtil;
import com.antis.olsl.library.MyApplication;
import com.antis.olsl.library.response.GetSalesroomOrWarehouseListRes;
import com.antis.olsl.library.utils.RSAEncryptUtil;
import com.antis.olsl.magic.net.ApiServer;
import com.antis.olsl.magic.net.RetrofitManager;
import com.antis.olsl.magic.response.GetTopAnalysisDetailResp;
import com.antis.olsl.magic.response.GetTopAnalysisTotalResp;

import java.util.Map;

import timber.log.Timber;

public class Top60Presenter implements Top60Contract.Presenter {
    private Top60Contract.View mView;
    private ApiServer mApiServer;

    Top60Presenter() {
        mApiServer = RetrofitManager.getInstance().getServerApi();
    }

    @Override
    public void takeView(Top60Contract.View view) {
        mView = view;
    }

    @Override
    public void getSalesroomInfoList(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        Timber.tag("hhh").e("getSalesroomInfoList map= " + map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        mApiServer.getSalesroomInfoList(encryptString, token, 2).compose(RxUtil.<GetSalesroomOrWarehouseListRes>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<GetSalesroomOrWarehouseListRes>bindToLife())
                .subscribe(new BaseSubscriber<GetSalesroomOrWarehouseListRes>() {
                    @Override
                    public void onNext(GetSalesroomOrWarehouseListRes resp) {
                        if (resp.isOk()) {
                            if (null != resp.getContent() && resp.getContent().size() > 0) {
                                mView.getSalesroomInfoListSuccess(resp.getContent());
                            }
                        } else if (resp.isTimeOut()) {
                            mView.getSalesroomInfoListFailure("请求超时");
                        } else if (resp.isSlowNetwork()) {
                            mView.getSalesroomInfoListFailure("网络缓慢，稍后重试");
                        } else if (resp.isUnauthorized()) {
                            mView.getSalesroomInfoListFailure("接口未授权");
                        } else {
                            mView.getSalesroomInfoListFailure("请求失败");
                        }
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        if (null != e) {
                            if (!TextUtils.isEmpty(e.message)) {
                                mView.getSalesroomInfoListFailure(e.message);
                            } else {
                                mView.getSalesroomInfoListFailure("请求失败");
                            }
                        } else {
                            mView.getSalesroomInfoListFailure("请求失败");
                        }
                    }
                });
    }

    @Override
    public void getTopAnalysisTotal(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        Timber.tag("hhh").e("getTopAnalysisTotal map= " + map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        mApiServer.getTopAnalysisTotal(encryptString, token, 2).compose(RxUtil.<GetTopAnalysisTotalResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<GetTopAnalysisTotalResp>bindToLife())
                .subscribe(new BaseSubscriber<GetTopAnalysisTotalResp>() {
                    @Override
                    public void onNext(GetTopAnalysisTotalResp resp) {
                        if (resp.isOk()) {
                            if (null != resp.content && resp.content.size() > 0) {
                                mView.getTopAnalysisTotalSuccess(resp.content);
                            } else {
                                mView.getTopAnalysisTotalFailure("数据为空");
                            }
                        } else if (resp.isTimeOut()) {
                            mView.getTopAnalysisTotalFailure("请求超时");
                        } else if (resp.isSlowNetwork()) {
                            mView.getTopAnalysisTotalFailure("网络缓慢，稍后重试");
                        } else if (resp.isUnauthorized()) {
                            mView.getTopAnalysisTotalFailure("接口未授权");
                        } else {
                            mView.getTopAnalysisTotalFailure("请求失败");
                        }
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        if (null != e) {
                            if (!TextUtils.isEmpty(e.message)) {
                                mView.getTopAnalysisTotalFailure(e.message);
                            } else {
                                mView.getTopAnalysisTotalFailure("请求失败");
                            }
                        } else {
                            mView.getTopAnalysisTotalFailure("请求失败");
                        }
                    }
                });
    }

    @Override
    public void getItemList(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        Timber.tag("hhh").e("getItemList map= " + map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        mApiServer.getTopAnalysisDetail(encryptString, token, 2).compose(RxUtil.<GetTopAnalysisDetailResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<GetTopAnalysisDetailResp>bindToLife())
                .subscribe(new BaseSubscriber<GetTopAnalysisDetailResp>() {
                    @Override
                    public void onNext(GetTopAnalysisDetailResp resp) {
                        if (resp.isOk()) {
                            if (null != resp.content && resp.content.size() > 0) {
                                mView.getItemListSuccess(resp.content);
                            } else {
                                mView.getItemListFailure("数据为空");
                            }
                        } else if (resp.isTimeOut()) {
                            mView.getItemListFailure("请求超时");
                        } else if (resp.isSlowNetwork()) {
                            mView.getItemListFailure("网络缓慢，稍后重试");
                        } else if (resp.isUnauthorized()) {
                            mView.getItemListFailure("接口未授权");
                        } else {
                            mView.getItemListFailure("请求失败");
                        }
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        if (null != e) {
                            if (!TextUtils.isEmpty(e.message)) {
                                mView.getItemListFailure(e.message);
                            } else {
                                mView.getItemListFailure("请求失败");
                            }
                        } else {
                            mView.getItemListFailure("请求失败");
                        }
                    }
                });
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        Timber.tag("Top60Presenter").e("unSubscribe");
        mView = null;
    }
}
